package com.lc.question.dao;

import com.lc.question.constant.MyConstants;
import com.lc.question.pojo.UserTab;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository("userTabDao")
public interface IUserTabDao {

    @Select("select * from usertab where user_Id=#{user_Id}")
    public List<UserTab> findAllMyTab(String user_Id);

    @Select("select * from usertab where pub_Id=#{pub_Id}")
    public List<UserTab> findAllPubTab(String pub_Id);

    //通过user_Id和tab_Id搜索
    @Select("select * from usertab where user_Id=#{user_Id} and plan_Id=#{plan_Id}")
    public List<UserTab> findTab_TIdUId(String user_Id,int plan_Id);

    @Insert("insert into usertab(user_Id,plan_Id,sur_state,par_state,create_time,over_time,t_text) " +
            "values(#{user_Id},#{plan_Id}," +
            "#{sur_state},#{par_state},#{create_time},#{over_time},#{t_text})")
    public void addUserTab(UserTab userTab);

    //更改用户的参与状态为已参与
    @Update("update usertab set par_state='"+ MyConstants.PAR_FINISHED +"' where user_Id=#{user_Id} and plan_Id=#{plan_Id}" )
    public void updateMyTabPar(String user_Id,int plan_Id);

    //更改用户的参与状态为已参与
    @Update("update usertab set over_time=#{arg1} where user_Id=#{arg0}" )
    public void updateOverTime(String user_Id, Date over_Time);

    //更改问卷表的状态为已结束
    @Update("update usertab set sur_state='"+ MyConstants.SUR_FINISHED +"' where plan_Id=#{plan_Id}" )
    public void updateTabSur(int plan_Id);

    //查询有关问卷表的所有用户
    @Select("select * from usertab where plan_Id=#{plan_Id}")
    public List<UserTab> findTab_TId(int plan_Id);

    //用户的问卷表--调查状态和参与状态改变时
    @Select("<script>" +
            "select * from usertab where 1=1 and user_Id=#{user_Id} " +
            "<if test='sur_state!=null and sur_state!=\"\"'>" +
                "and sur_state=#{sur_state}"+
            "</if>"+
            "<if test='par_state!=null and par_state!=\"\"'>" +
                "and par_state=#{par_state}"+
            "</if>"+
            "</script>")
    public List<UserTab> findUserChange_SurPar(UserTab userTab);


    //用户的问卷表--参与状态改变时
    @Select("<script>" +
            "select count(*) from usertab where 1=1 and tab_Id=#{tab_Id} " +
            "<if test='par_state!=null and par_state!=\"\"'>" +
                "and par_state=#{par_state}"+
            "</if>"+
            "</script>")
    public int tabTotal(UserTab userTab);

    //用户的问卷表--参与状态改变时
    @Select("<script>" +
            "select * from usertab where 1=1 and userTab.plan_Id=#{userTab.plan_Id} " +
            "<if test='userTab.par_state!=null and userTab.par_state!=\"\"'>" +
                "and userTab.par_state=#{userTab.par_state}"+
            "</if>"+
                "order by over_time desc limit #{arg1},#{arg2}"+
            "</script>")
    public List<UserTab> findTabChange_SurPar(@Param("userTab") UserTab userTab,int page,int total);


    @Delete("delete * from usertab where plan_Id=#{plan_Id}")
    public void deleteUserTab(int plan_Id);

    @Delete("delete * from usertab where plan_Id=#{plan_Id} and user_Id=#{user_Id}")
    public void deleteUserTab2(int plan_Id,String user_Id);

    //更改用户的参与状态为已参与
    @Update("update usertab set par_state='"+ MyConstants.PAR_FINISHED +"' where user_Id=#{user_Id}" )
    public void updateUserTab(String user_Id);

}
















